package com.zmkj.taobao.taobaoorderservice.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.zmkj.taobao.taobaoorderservice.apiresponse.BaseResponse;
import com.zmkj.taobao.taobaoorderservice.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/testuser")
public class TestUserController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    EurekaClient eurekaClient;

    @GetMapping("list")
    public String list(){
        //获取eureka服务信息
        InstanceInfo instanceInfo= eurekaClient.getNextServerFromEureka("TAOBAO-USERSERVICE",false);
        // 获取调用服务地址
        String url=instanceInfo.getHomePageUrl()+"/api/user/list";
        //restTemplate调用接就获取返回数据
        ResponseEntity result= restTemplate.getForEntity(url, BaseResponse.class);
        return result.toString();
    }
    //根据用户名查询
    @GetMapping("/queryUserByName")
    public String queryUserByName(){
        //获取eureka服务信息
        InstanceInfo instanceInfo= eurekaClient.getNextServerFromEureka("TAOBAO-USERSERVICE",false);
        // 获取调用服务地址
        String url=instanceInfo.getHomePageUrl()+"/api/user/queryUserByUserName/admin";
        //restTemplate调用接就获取返回数据
        ResponseEntity  result= restTemplate.getForEntity(url,BaseResponse.class);

        return result.toString();
    }

    //根据用户名和密码查询  传统get方式传多个参数
    @GetMapping("/queryUserByNameAndPwd")
    public String queryUserByNameAndPwd(){
        //获取eureka服务信息
        InstanceInfo  instanceInfo= eurekaClient.getNextServerFromEureka("TAOBAO-USERSERVICE",false);
        // 获取调用服务地址
        String url=instanceInfo.getHomePageUrl()+"/api/user/queryUserByUserNameAndPwd?userName={userName}&password={password}";
        Map<String,Object>  map=new HashMap<>();
        map.put("userName","wll");
        map.put("password","123");
        //restTemplate调用接就获取返回数据
        ResponseEntity  result= restTemplate.getForEntity(url,BaseResponse.class,map);
        return result.toString();
    }
    //删除用户信息接口
    @DeleteMapping("/remove")
    public  String   remove(){
        //获取eureka服务信息
        InstanceInfo  instanceInfo= eurekaClient.getNextServerFromEureka("TAOBAO-USERSERVICE",false);
        // 获取调用服务地址
        String url=instanceInfo.getHomePageUrl()+"/api/user/remove/121";

        //restTemplate调用接就获取返回数据
        restTemplate.delete(url);
        return "修改成功";
    }

        @PostMapping("/addUser")
    public  String   addUser(){
        //获取eureka服务信息
        InstanceInfo  instanceInfo= eurekaClient.getNextServerFromEureka("TAOBAO-USERSERVICE",false);
        // 获取调用服务地址
        String url=instanceInfo.getHomePageUrl()+"/api/user/addUser";
        User user=new User();
        user.setId(5);
//        user.setUserName("zhb");
        user.setRealName("张志博");
        user.setPassword("123");
        user.setAddress("河南洛阳");
        user.setMobile("12314252656");
        user.setEmail("djsjfsjgj@163.com");
        //restTemplate调用接就获取返回数据
        ResponseEntity  result= restTemplate.postForEntity(url,user,BaseResponse.class);
        //第二种调用方式
//        Object result2=restTemplate.postForObject(url,user,boolean.class);
        return result.toString();
    }
    @PutMapping("/updateUser")
    public  String   updateUser(){
        //获取eureka服务信息
        InstanceInfo  instanceInfo= eurekaClient.getNextServerFromEureka("TAOBAO-USERSERVICE",false);
        // 获取调用服务地址
        String url=instanceInfo.getHomePageUrl()+"/api/user/updateUser";
        User  user=new User();
        user.setUserName("zhb");
        user.setId(19);
        user.setRealName("张志博");
        user.setPassword("123456");
        user.setAddress("河南洛阳洛龙区");
        user.setMobile("12314252656");
        user.setEmail("djsjfsjgj@163.com");
        //restTemplate调用接就获取返回数据
        restTemplate.put(url,user);
        return "修改成功";
    }

}
